package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.dto.CourseQueryDTO;
import com.jeff.tianti.cms.dto.TeachQueryDTO;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.entity.Course;
import com.jeff.tianti.cms.entity.Teach;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.cms.service.CourseService;
import com.jeff.tianti.cms.service.TeachService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    CourseService courseService;

    @Autowired
    ColumnInfoService columnInfoService;

    @Autowired
    TeachService teachService;

    @RequestMapping("/list")
    public String index(HttpServletRequest request, Model model)throws Exception {
        String name = request.getParameter("name");
        String isBest = request.getParameter("isBest");
        String columnId = request.getParameter("columnId");
        String startTimeStr = request.getParameter("startTime");
        String endTimeStr = request.getParameter("endTime");
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");

        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        CourseQueryDTO courseQueryDTO = new CourseQueryDTO();
        courseQueryDTO.setName(name);
        courseQueryDTO.setIsBest(isBest);
        courseQueryDTO.setColumnId(columnId);
        courseQueryDTO.setStartTime(null);
        courseQueryDTO.setEndTime(null);
        courseQueryDTO.setCurrentPage(currentPage);
        courseQueryDTO.setPageSize(pageSize);

        if (StringUtils.isNotBlank(startTimeStr)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startTime = sdf.parse(startTimeStr);
            courseQueryDTO.setStartTime(startTime);
        }
        if (StringUtils.isNotBlank(endTimeStr)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date endTime = sdf.parse(endTimeStr);
            courseQueryDTO.setEndTime(endTime);
        }
        PageModel<Course> page = courseService.queryCoursePage(courseQueryDTO);
        List<Course> list = page.getList();
        list.forEach(course -> {
            String cId = course.getColumnId();
            ColumnInfo columnInfo =null;
            if (!StringUtils.isBlank(cId)) {
                 columnInfo = columnInfoService.find(cId);
            }
            if (columnInfo != null) {
                course.setCourseCateName(columnInfo.getName());
            }

        });
        model.addAttribute("page", page);
        model.addAttribute("courseQueryDTO", courseQueryDTO);
        model.addAttribute("startTimeStr", startTimeStr);
        model.addAttribute("endTimeStr", endTimeStr);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_COURSE);
        return "/cms/course/course_list";
    }

    @RequestMapping("/edit")
    public String dialogEdit(HttpServletRequest request, Model model) {

        String id = request.getParameter("id");
        List<ColumnInfo> rootCoulumnInfo = columnInfoService.findByCode(Constants.CODE_KCFL);
        String rootColumnId = "";
        if (!CollectionUtils.isEmpty(rootCoulumnInfo)) {
            ColumnInfo columnInfo = rootCoulumnInfo.get(0);
            rootColumnId = columnInfo.getId();
        }
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setRootColumnId(rootColumnId);
        columnInfoQueryDTO.setIsRootColumnLike(false);
        List<ColumnInfo> rootCoulumnInfoList = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);

        List<Teach> teachList = teachService.findAllNoDel();

        if (StringUtils.isNotBlank(id)) {
            Course course = courseService.find(id);
            model.addAttribute("course", course);
        }
        model.addAttribute("rootCoulumnInfoList", rootCoulumnInfoList);
        model.addAttribute("teachList", teachList);
        return "/cms/course/dialog/course_edit";
    }


    @RequestMapping("/ajax/save")
    @ResponseBody
    public AjaxResult doFriendShipAjaxSave(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {

            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String columnId = request.getParameter("columnId");
            String description = request.getParameter("description");
            String applyTo = request.getParameter("applyTo");
            String deleteFlag = request.getParameter("deleteFlag");
            String startTime = request.getParameter("startTime");
            String isBest = request.getParameter("isBest");
            String coverImg = request.getParameter("coverImg");
            String rootColumnId = request.getParameter("rootColumnId");
            String teachId = request.getParameter("teachId");
            Date date =null;
            if (StringUtils.isNotBlank(startTime)){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                 date = sdf.parse(startTime);
            }
            Course course = new Course();
            course.setName(name);
            course.setId(id);
            course.setDescription(description);
            course.setDeleteFlag("0");
            course.setColumnId(columnId);
            course.setApplyTo(applyTo);
            course.setStartTime(date);
            course.setIsBest(isBest);
            course.setCoverImg(coverImg);
            course.setRootColumnInfoId(rootColumnId);
            course.setTeachId(teachId);
            if (StringUtils.isNotBlank(id)) {
                courseService.updateSelectiveById(course);
            } else {
                courseService.save(course);
            }
            ajaxResult.setSuccess(true);

        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return ajaxResult;
    }

    /**
     * 栏目保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/del")
    @ResponseBody
    public AjaxResult doFriendShipAjaxDel(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            courseService.delete(id);
            ajaxResult.setSuccess(true);

        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return ajaxResult;
    }

}
